(function () {
    function PublicController() {
        

        viewer.register("arena.start", this.start.bind(this));
        viewer.register("game::getUserStep", this.setUserStep.bind(this));
        
        viewer.register("io.reconnect", this.connectError.bind(this));
        viewer.register("io.disconnect", this.connectError.bind(this));

        viewer.register("cmd.error", this.errorHandler.bind(this));
        viewer.register("cmd::caution", this.caution.bind(this));
    }
    var _proto = PublicController.prototype;

    _proto.caution = function (data) {
        //输分禁用
        if(data.code == "1000"){
            GM.jumpToHomePage && GM.jumpToHomePage("blacklist_disable");
        }

        //万里通积分授权
        if(data.code == "1001"){
            GM.accredit && GM.accredit();
        }

        viewer.publish("room::leaveTable");
    }
    /**
     * @public
     * 根据获取的用户当前位置设置进入的模块
     * 
     * @param {Object} res 用户当前位置,1:表示未登录 2:表示登录在大厅 3:表示登录在房间
     */
    _proto.setUserStep = function (res){
        var status = res.status;

        if(Arena.SOUNDSTATUS.CURRENT === Arena.SOUNDSTATUS.DISABLE){
            Laya.SoundManager.musicMuted = true;
            Laya.SoundManager.soundMuted = true;
        }

        switch(status | 0){
            case 1:
            case 2:
                viewer.publish("loader.load", "home");
                break;
            case 3:
                viewer.publish("loader.load", "room");
                break;
        }

    }
    /**
     * @public
     * Socket连接错误，弹出网络异常弹窗
     */
    _proto.connectError = function () {
        var param = {
            message : "您的网络连接不稳定，是否继续尝试重连",
            closeHandler : function () {
                location.reload();
            }
        }
        viewer.publish("alert.show", "normal", param);
    }
    /**
     * @public
     * 游戏开始时监听arena.start消息，获取用户当前位置
     */
    _proto.start = function () {
        if(Arena.SOCKET.CONNECTED){
            viewer.publish("io.emit", {
                cmd : "game::getUserStep",
                params : {
                    token : token
                }
            });
        }else{
            viewer.register("io.connect", function () {
                viewer.publish("io.emit", {
                    cmd : "game::getUserStep",
                    params : {
                        token : token
                    }
                });
            });
        }
    }
    /**
     * @public
     * 接收cmd.error消息
     */
    _proto.errorHandler = function (cmd, code, msg) {
        switch(code){
            case 303:
                var param = {
                    message : "对不起，您的账户已被冻结，无法进行此游戏。如有疑问，请联系客服：4001081768。"
                }
                viewer.publish("alert.show", "normal", param);
                break;
            case 305:
                var param = {
                    message : "很抱歉！经系统检测，您的账号存在异常，无法进行此游戏。如有疑问，请联系客服：4001081768。"
                }
                viewer.publish("alert.show", "normal", param);
                break;
                
            case 307:
                viewer.publish("alert.tip", "对不起，您还在游戏中，暂时无法兑出游戏币", 3000, 0.7, [0.7, 0.7]);
                break;
            case 308:
                viewer.publish("alert.tip", "对不起，暂时无法收获游戏币，请稍后重试", 3000, 0.7, [0.7, 0.7]);
                break;

            case 501:
                viewer.publish("alert.tip", "没有此预设的聊天语", 3000, 0.7, [0.7, 0.7]);
                break;

            case 100:
            case 1001:
                location.href = GM.userLoginUrl;
                break;

            case 301:
                var param = {
                    message : msg,
                    closeHandler : function () {
                        viewer.publish("room::leaveTable");
                    }
                }
                viewer.publish("alert.show", "normal", param);
                break;
            case 306:
                if(cmd == "room::into"){
                    if(GM.appStorePay == 1 && (window.wltgame && wltgame.suportStorePay == 1) || GM.appStorePay == 2){
                        setTimeout(function() {
                            location.href = "/?act=ios_applepay&st=applepay_game_vertical&gameId=" + GM.gameId + "&redirect_uri=" + redirect_uri;
                        }, 2000);
                    }else{
                        viewer.publish("alert.show", "recharge");
                    }
                    viewer.publish("alert.tip", "您的游戏币和余额不足，请充值后继续游戏", 3000, 0.7, [0.7, 0.7]);
                }else{
                    viewer.publish("alert.tip", "您的余额不足，暂无法使用", 3000, 0.7, [0.7, 0.7]);
                }
                break;
            case 1002:
                var param = {
                    message : "您已经在别处登录。",
                    closeType : 1,
                    closeHandler : function () {
                        //viewer.publish("io.disconnect");
                        location.reload();
                    }
                }
                viewer.publish("alert.show", "normal", param);
                break;
            case 208:
                viewer.publish("alert.tip", "大爷们，本局比赛一不小心流局了，本局不产生结算。", 3000, 0.7, [0.7, 0.7]);
                break;

            //道具相关
            case 601:
                viewer.publish("alert.tip", "此道具无法购买", 3000, 0.7, [0.7, 0.7]);
                break;
            case 602:
                viewer.publish("alert.tip", "您的游戏币不足,无法购买此道具", 3000, 0.7, [0.7, 0.7]);
                break;
            case 603:
                viewer.publish("alert.tip", "购买道具失败！", 3000, 0.7, [0.7, 0.7]);
                break;
            case 100002:
                viewer.publish("alert.tip", "您的余额不足，暂无法使用", 3000, 0.7, [0.7, 0.7]);
                break;
        }
    }

    new PublicController();
})();